/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.hisense.settings.util;

import com.google.android.tv.settings.TvSettings;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Wrapper for Launcher preferences.
 */
public final class PreferenceUtils {

    /**
     * System property key for retrieving the boot token.
     */
    public static final String RO_BOOT_TOKEN = "ro.boot.token";

    private static final String PREF_KEY_BOOT_TOKEN = "boot_token";

    private static final String PREF_KEY_LAUNCHED_TUTORIAL = "launched_tutorial";

    /**
     * Preference key for storing the hide app list.
     * <p>
     * Public so that other classes can watch for changes in the list.
     */
    public static final String PREF_KEY_HIDE_APP_LIST = "hide_apps_list_internal";

    private PreferenceUtils() {}

    public static String getBootToken(SharedPreferences preferences) {
        return preferences.getString(PREF_KEY_BOOT_TOKEN, null);
    }

    public static void setBootToken(SharedPreferences preferences, String bootToken) {
        preferences.edit()
            .putString(PREF_KEY_BOOT_TOKEN, bootToken)
            .apply();
    }

    public static Intent getBootIntent(ContentResolver resolver) {
        String intentString = android.provider.Settings.System.getString(resolver,
                TvSettings.System.BOOT_INTENT);
        if (intentString != null) {
            try {
                return Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME);
            } catch (URISyntaxException e) {
                return null;
            }
        }
        return null;
    }

    public static void setBootIntent(ContentResolver resolver, Intent intent) {
        String intentString = intent != null ? intent.toUri(Intent.URI_INTENT_SCHEME) : null;
        android.provider.Settings.System.putString(resolver, TvSettings.System.BOOT_INTENT,
                intentString);
    }

    public static void setHiddenApps(SharedPreferences preferences, Set<String> hiddenApps) {
        preferences.edit()
                .putStringSet(PREF_KEY_HIDE_APP_LIST, hiddenApps)
                .apply();
    }

    public static boolean isAppHidden(SharedPreferences preferences, String intentUri) {
        Set<String> values = preferences.getStringSet(PREF_KEY_HIDE_APP_LIST, null);
        if (values == null) {
            return false;
        } else {
            return values.contains(intentUri);
        }
    }
    
    public static void setAppHidden(SharedPreferences preferences, Intent intent) {
        Set<String> values = preferences.getStringSet(PREF_KEY_HIDE_APP_LIST,
                new HashSet<String>());
        // Need to clone otherwise it would not be written to disk.
        values = (Set<String>) ((HashSet<String>) values).clone();
        values.add(intent.toUri(Intent.URI_INTENT_SCHEME));
        setHiddenApps(preferences, values);
    }

    public static boolean hasLaunchedTutorial(SharedPreferences preferences) {
        return preferences.getBoolean(PREF_KEY_LAUNCHED_TUTORIAL, false);
    }

    public static void setHasLaunchedTutorial(SharedPreferences preferences) {
        preferences.edit()
                .putBoolean(PREF_KEY_LAUNCHED_TUTORIAL, true)
                .apply();
    }
}

